import { getSetting, chooseAddress, openSetting, showModal, showToast } from '../../utils/asyncWX'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    allChecked: true,
    totalNum: 0,
    totalPrice: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  onShow() {
    const address = wx.getStorageSync('address')
    const cart = wx.getStorageSync('cart') || []
    this.setData({
      address
    })
    this.setCart(cart)
  },
  async addressAdd() {
    try {
      const res1 = await getSetting()
      const scopeAddress = res1.authSetting['scope.address']
      if (scopeAddress === false)
        await openSetting()
      let address = await chooseAddress()
      address.detail = address.provinceName + address.cityName + address.countyName + address.countyName + address.detailInfo
      wx.setStorageSync('address', address)
    } catch (err) {
      console.log(err)
    }
    // wx.getSetting({
    //   success: (result) => {
    //     const scopeAddress = result.authSetting['scope.address']
    //     if (scopeAddress === false) {
    //       wx.openSetting({
    //         success: (result1) => {
    //           wx.chooseAddress({
    //             success: (result3) => {

    //             }
    //           })
    //         }
    //       })
    //     } else { // true || undefind
    //       wx.chooseAddress({
    //         success: (result2) => {

    //         }
    //       })
    //     }
    //   },
    //   fail: () => {},
    //   complete: () => {}
    // })
  },
  // 复选框改变
  handelChange(e) {
    const goods_id = e.currentTarget.dataset.id
    let { cart } = this.data
    const index = cart.findIndex(item => item.goods_id === goods_id)
    cart[index].isChecked = !cart[index].isChecked
    this.setCart(cart)
  },
  handelAllChange() {
    let { cart, allChecked } = this.data
    allChecked = !allChecked
    cart.forEach(item => item.isChecked = allChecked)
    this.setCart(cart)
  },
  // 加减按钮
  async editNum(e) {
    const { id, operation } = e.currentTarget.dataset
    let { cart } = this.data
    const index = cart.findIndex(item => item.goods_id === id)
    if (operation === -1 && cart[index].num === 1) {
      const res = await showModal({content: '是否删除商品?'})
      if (res.confirm) {
        cart.splice(index, 1)
      } 
    } else {
      cart[index].num += operation
    }
    this.setCart(cart)
  },
  // 计算
  setCart(cart) {
    let totalNum = 0, totalPrice = 0, allChecked = true
    cart.forEach(item => {
      if (item.isChecked) {
        totalNum += item.num
        totalPrice += item.num * item.goods_price
      } else {
        allChecked = false
      }
    })
    allChecked = cart.length != 0 ? allChecked : false
    this.setData({
      cart,
      allChecked,
      totalNum,
      totalPrice
    })
    wx.setStorageSync('cart', cart)
  },
  // 支付
  async handelPay() {
    const { totalNum, address } = this.data
    if (totalNum === 0) {
      await showToast({title: '请选择商品'})
      return
    }
    if (!address.userName) {
      await showToast({title: '请添加收货地址'})
      return
    }
    // 跳转支付页面
    wx.navigateTo({
      url: '/pages/pay/index'
    })
  }
})